import request from '@/utils/request'
import type { BannerGroup, BannerGroupForm, BannerItem, BannerItemForm, PaginationResponse, BannerPushRecord } from '@/types'

// 获取轮播图组列表
export const getBannerGroupList = (params: any): Promise<PaginationResponse<BannerGroup>> => {
  return request.get('/content/banner-groups', { params })
}

// 创建轮播图组
export const createBannerGroup = (data: BannerGroupForm): Promise<void> => {
  return request.post('/content/banner-groups', data)
}

// 更新轮播图组
export const updateBannerGroup = (id: number, data: BannerGroupForm): Promise<void> => {
  return request.put(`/content/banner-groups/${id}`, data)
}

// 删除轮播图组
export const deleteBannerGroup = (id: number): Promise<void> => {
  return request.delete(`/content/banner-groups/${id}`)
}

// 获取轮播图项列表
export const getBannerItemList = (groupId: number): Promise<BannerItem[]> => {
  return request.get(`/content/banner-groups/${groupId}/items`)
}

// 添加轮播图项
export const addBannerItem = (groupId: number, data: BannerItemForm): Promise<void> => {
  return request.post(`/content/banner-groups/${groupId}/items`, data)
}

// 更新轮播图项
export const updateBannerItem = (groupId: number, itemId: number, data: BannerItemForm): Promise<void> => {
  return request.put(`/content/banner-groups/${groupId}/items/${itemId}`, data)
}

// 删除轮播图项
export const deleteBannerItem = (groupId: number, itemId: number): Promise<void> => {
  return request.delete(`/content/banner-groups/${groupId}/items/${itemId}`)
}

// 推送轮播图组到设备
export const pushBannerGroup = (groupId: number, deviceIds: number[], settings: {
  pushTime: 'immediate' | 'schedule'
  scheduleTime?: string
  pushMode: 'replace' | 'append'
}): Promise<void> => {
  return request.post(`/content/banner-groups/${groupId}/push`, {
    deviceIds,
    ...settings
  })
}

// 获取推送记录
export const getPushRecords = (params: any): Promise<PaginationResponse<BannerPushRecord>> => {
  return request.get('/content/push-records', { params })
}

// 上传图片
export const uploadImage = (file: File): Promise<{ url: string }> => {
  const formData = new FormData()
  formData.append('file', file)
  return request.upload('/content/upload', formData)
}

// 获取新闻列表
export const getNewsList = (params: any): Promise<any> => {
  return request.get('/content/news', { params })
}

// 创建新闻
export const createNews = (data: any): Promise<void> => {
  return request.post('/content/news', data)
}

// 更新新闻
export const updateNews = (id: number, data: any): Promise<void> => {
  return request.put(`/content/news/${id}`, data)
}

// 删除新闻
export const deleteNews = (id: number): Promise<void> => {
  return request.delete(`/content/news/${id}`)
}

// 获取公告列表
export const getAnnouncementList = (params: any): Promise<any> => {
  return request.get('/content/announcements', { params })
}

// 创建公告
export const createAnnouncement = (data: any): Promise<void> => {
  return request.post('/content/announcements', data)
}

// 更新公告
export const updateAnnouncement = (id: number, data: any): Promise<void> => {
  return request.put(`/content/announcements/${id}`, data)
}

// 删除公告
export const deleteAnnouncement = (id: number): Promise<void> => {
  return request.delete(`/content/announcements/${id}`)
}
